<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          required
          label="规则名称">
          <a-input placeholder="请输入规则名称" v-model="model.ruleName" />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="roleCode"
          label="角色名称">
          <j-select-role v-model="model.roleCode" @select="changeRole" :multiple="false"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import {ruleAdd} from '@/api/warning/Policy.js'
  import JDictSelectTag from '@/components/dict/JDictSelectTag'
  import JSelectRole from '@/components/jeecgbiz/JSelectRole'

  export default {
    name: 'RuleModal',
    components: { JDictSelectTag ,JSelectRole},
    data() {
      return {
        title: '操作',
        visible: false,
        model: {
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          roleCode: [{ required: true, message: '请角色角色' }] ,
        },
        readOnly:false
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit(this.model)
      },
      edit(record) {
        this.model = Object.assign({}, record)
        this.visible = true
        if(record.id){
          this.readOnly=true
        }else{
          this.readOnly=false
        }
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.$refs.form.resetFields();
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true
            ruleAdd(this.model).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message?res.message:'请求失败！')
              }
            }).catch(e=>{
              this.$message.warning("请求失败！");
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }else{
            return false;
          }
        })
      },
      handleCancel() {
        this.close()
      },
      changeRole(data) {
        if(data && data[0]){
          this.model.roleName = data[0].roleName
          this.model.roleId = data[0].id
        }else{
          this.model.roleName = ''
          this.model.roleId = ''
        }
        console.log("this.model",this.model);
      },

    }
  }
</script>

<style lang="less" scoped>

</style>
